package ig.util;


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;

public class ConexionServer {
    private String SocketServer;
    public ConexionServer(){
        SocketServer = SocketCon();
    }
    protected String SocketCon(){
        String host =null;
        String pto = null;
        boolean propiedadesXML=false;
        
        File file = new File("../server/server/configuracion.properties");
        String FICHERO_CONFIGURACION = "../server/server/configuracion.properties";
        if(!file.exists()){
            FICHERO_CONFIGURACION = "./server/configuracion.properties";
            file = new File(FICHERO_CONFIGURACION);
            if(!file.exists()){
                try {
                    Context initialContext;        
                    Context environment;
                    initialContext = new InitialContext();                    
                    environment = (Context) initialContext.lookup("java:comp/env");                    
                    host=(String) environment.lookup("_host_");
                    pto=(String) environment.lookup("PORT_JNDI");
                    propiedadesXML=true;
                } catch (Exception ex) {
                    ex.printStackTrace();
//                    throw ex;
                }
            }
        }
        if(!propiedadesXML){
            Properties propiedades;
            try {
                FileInputStream f = new FileInputStream(FICHERO_CONFIGURACION);
                propiedades = new Properties();
                propiedades.load(f);
                f.close();
                host = propiedades.getProperty("host");
                pto = propiedades.getProperty("puerto");
            } catch (Exception e) {
                e.printStackTrace();
//                throw e;
            }
        }
        return host+":"+pto;
    }
    
    public String getSocketServer() {
        return SocketServer;
    }

    public void setSocketServer(String socketServer) {
        SocketServer = socketServer;
    }
}

